﻿/// <reference path="../_references.js" />
(function (e) {

    e.view = e.view || {};

    e.view.define = function (obj) {
        var constr = function (cont) {
            this.cont = cont;
        };
        constr.prototype = $.extend({
            render: function (model) {
                model = model || this._getViewModel();
                model = model || this;
                this.cont.html($("#" + this.templateName).html());
                ko.applyBindings(model, this.cont[0]);
                if (Object.hasFunction(this, "afterRender"))
                    this.afterRender();
            },
            _load: function (state) {
                if (Object.hasFunction(this, "load"))
                    this.load(state);
            },
            _refresh: function (state) {
                if (Object.hasFunction(this, "refresh"))
                    this.refresh(state);
            },
            _unload: function () {
                if (Object.hasFunction(this, "unload"))
                    this.unload();
                ko.cleanNode(this.cont[0]);
            },
            _getViewModel: function () {
                if (Object.hasFunction(this, "getViewModel"))
                    return this.getViewModel();
                return this;
            }
        }, obj);
        constr.prototype.constructor = e.view;
        return constr;
    };

} (this.eg = this.eg || {}));